package com.me.batm;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Vector;

import aurelienribon.tweenengine.Tween;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Rectangle;
import com.me.batm.models.Target;
import com.me.batm.screens.DisplayImageScreen;
import com.me.batm.screens.MainScreen;
import com.me.batm.screens.TestScreen;
import com.me.batm.tweens.BasicTweenAccessor;
import com.me.batm.tweens.TargetAccessor;

//Game Finished :):)++

public class OurGdxGame extends Game{
private static final String TAG = OurGdxGame.class.getSimpleName();
	
	@Override
	public void create() {
		Gdx.app.setLogLevel(Application.LOG_DEBUG);
		Tween.registerAccessor(Rectangle.class, new BasicTweenAccessor());
		Tween.registerAccessor(Target.class, new TargetAccessor());

		loadDefaultPreferences();
		loadPropertiesFile();
		loadGameConfiguration();
		
		String actualUser = Gdx.app.getPreferences(GameConstants.GAME_NAME).getString("actual_user");
		
		if(actualUser.contains("gareve"))
			setScreen(new TestScreen(this));
		if (actualUser.contains("mauricio"))
			setScreen(new MainScreen(this,actualUser));			
		if (actualUser.contains("vudduu"))
			setScreen( new MainScreen(this,actualUser));
		if (actualUser.contains("thaer"))
			setScreen( new MainScreen(this,actualUser));
		if (actualUser.contains("alvaro") || actualUser.contains("android")) {
			Vector<String> filePath = new Vector<String>();
			filePath.add("data/headphonesalert.png");
			filePath.add("data/logosplash.png");
			filePath.add("data/presents.png");
			//filePath.add("data/troll.png");
			//setScreen(new GameOverScreen(this));
			setScreen(new DisplayImageScreen(this, GameConstants.MENU_SCREEN,
					filePath, 3000));
		}
	}

	private void loadDefaultPreferences(){
		Gdx.app.getPreferences(GameConstants.GAME_NAME).putString("actual_user", "alvaro");
		Gdx.app.getPreferences(GameConstants.GAME_NAME).putString("difficulty", "normal");
		Gdx.app.getPreferences(GameConstants.GAME_NAME).putInteger("actual_level", 1);
	}

	private void loadPropertiesFile(){
		Properties prop = new Properties();
		String propertiesPath = String.format("%s/game.properties", System.getProperty("user.home"));
		Gdx.app.log(TAG,String.format("Properties Path = [%s]",propertiesPath));
		
		try {
			InputStream stream = new FileInputStream(propertiesPath);
			prop.load(stream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for (Entry<Object, Object> entry : prop.entrySet()) {
			//Gdx.app.log(TAG,String.format("%s = [%s]",entry.getKey(),entry.getValue()));
			Gdx.app.getPreferences(GameConstants.GAME_NAME).putString((String)entry.getKey(), (String)entry.getValue());
		}
		//Gdx.app.getPreferences(GameConstants.GAME_NAME).flush();
		
		Gdx.app.log(TAG,"Game Properties");
		for(Entry entry : Gdx.app.getPreferences(GameConstants.GAME_NAME).get().entrySet()){
			Gdx.app.log(TAG,String.format("[%s] = [%s]",entry.getKey(),entry.getValue()));
		}
	}
	
	private void loadGameConfiguration(){
		Properties prop = new Properties();		
		try {
			FileHandle gameProperties = Gdx.files.internal("game_config.properties");
			prop.load(gameProperties.read());
		} catch (Exception e) {
			e.printStackTrace();
		}

		for (Entry<Object, Object> entry : prop.entrySet()) {
			//Gdx.app.log(TAG,String.format("%s = [%s]",entry.getKey(),entry.getValue()));
			Gdx.app.getPreferences(GameConstants.GAME_NAME).putFloat((String)entry.getKey(), Float.parseFloat((String)entry.getValue()));
		}
		//Gdx.app.getPreferences(GameConstants.GAME_NAME).flush();

		Gdx.app.log(TAG,"Game Configuration");
		for(Entry entry : Gdx.app.getPreferences(GameConstants.GAME_NAME).get().entrySet()){
			Gdx.app.log(TAG,String.format("[%s] = [%s]",entry.getKey(),entry.getValue()));
		}
	}
}
